package edu.gdut.thesissys.action.director;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.gdut.thesissys.action.BaseAction;
import edu.gdut.thesissys.service.TeacherService;

public class FindTeacherAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5950177622105409857L;
	private static final Logger log = LoggerFactory
			.getLogger(FindTeacherAction.class);
	@Resource(name = "teacherService")
	private TeacherService teacherService;

	private Object result = new Object();// 查询结果

	private Integer offset;// 开始位置
	private Integer pagesize;// 查询记录总数
	private String id;// 教师工号

	/**
	 * 查找教师
	 * 
	 * @return
	 */
	public String findTeacher() {
		log.info("find teacher by id " + id);
		this.result = teacherService.findTeacherJson(id);
		log.info("find teacher successful and return " + result.toString());
		return SUCCESS;
	}

	/**
	 * 查找全部教师
	 * 
	 * @return
	 */
	public String findAllTeachers() {
		log.info("find all teachers");
		this.result = teacherService.findTeachersJson(offset, pagesize);
		log
				.info("find all teachers successful and return "
						+ result.toString());
		return SUCCESS;
	}

	public void validateFindTeacher() {
		if (id == null || id.trim().equals("")) {
			this.addActionError("error param");
			log.info("find teacher failed for error param");
		}
	}

	public void validateFindAllTeachers() {
		if (offset == null || pagesize == null || offset < 0 || pagesize < 1) {
			this.addActionError("error params");
			log.info("find all teacher failed for error params");
		}
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	@JSON(serialize = false)
	public Integer getOffset() {
		return offset;
	}

	public void setPagesize(Integer pagesize) {
		this.pagesize = pagesize;
	}

	@JSON(serialize = false)
	public Integer getPagesize() {
		return pagesize;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public Object getResult() {
		return result;
	}

	@JSON(serialize = false)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
